package cn.com.surker.lab.controller;

import cn.com.surker.base.BaseEntity;
import cn.com.surker.common.AbstractController;
import cn.com.surker.lab.service.ErpLabExpendBatchService;
import cn.com.surker.lab.service.LabExpendService;
import cn.com.surker.lab.vo.LabExpendBatchVo;
import cn.com.surker.lab.vo.LabExpendVo;
import cn.com.surker.production.entity.ProductionInvoice;
import cn.com.surker.production.service.ProductionInvoiceService;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.util.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import cn.com.surker.spring.RestResultDto;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工控机消耗表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-11-05
 */

@RestController
@RequestMapping("/lab-expend")
public class LabExpendController extends AbstractController {

	@Resource
	private LabExpendService expendService;
	@Autowired
	private ProductionInvoiceService invoiceService;
	@Autowired
	private ErpLabExpendBatchService expendBatchService;
	@Value("${erp.datasource.erp.db}")
	private String erpDb;

	/**
	 * 按强度查询原材平均消耗
	 * @param searchVal
	 * @param lineId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/strength-dosage", method = RequestMethod.POST)
	public RestResultDto findStrengthDosage(String searchVal, Long lineId, String startTime, String endTime) {
		List<Map<String, Object>> expendVoList = expendService.findStrengthDosage(erpDb, searchVal, null,
				lineId, startTime, endTime);
		if (expendVoList == null || expendVoList.size() == 0) {
			return RestResultDto.fail("没有任何数据");
		}
		return RestResultDto.success(expendVoList);
	}
	/**
	 * 料仓消耗统计
	 * @param orgId
	 * @param lineId 生产线ID
	 * @param storageId 仓位ID
	 * @param materialId 材料ID
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/storage-expend", method = RequestMethod.POST)
	public RestResultDto storageExpend(Long orgId, Long lineId, Long storageId, Long materialId,
									 String startTime, String endTime, Integer type, Integer materialType, Integer breedType) {
		List<LabExpendVo> expendVoList = expendService.findStorageExpend(orgId, lineId, storageId, materialId,
				startTime, endTime, type,materialType, breedType);
		if (expendVoList == null || expendVoList.size() == 0) {
			return RestResultDto.fail("没有任何数据");
		}
		return RestResultDto.success(expendVoList);
	}
	/**
	 * 导出砼强度平均消耗统计明细
	 * @param orgId
	 * @param lineId 生产线ID
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/storage-dosage-export", method = RequestMethod.POST)
	public RestResultDto storageDosageExpend(HttpServletRequest request, HttpServletResponse response, Long orgId,
											 String searchVal, Long lineId, String startTime, String endTime, String orgName) {
		try {
			List<Map<String, Object>> expendVoList = expendService.findStrengthDosage(erpDb, searchVal, null,
					lineId, startTime, endTime);
			if (expendVoList == null || expendVoList.size() == 0) {
				return RestResultDto.fail("没有任何数据");
			}
			CacheUser user= getUser();
			String sheetName = orgName + "砼强度平均消耗统计明细表(" + startTime.substring(0,10) + "~" + endTime.substring(0, 10) + ")";
			String title = "制表人:" + user.getName() + ",导出时间:" + DateUtil.convertString(new Date());
			XSSFWorkbook hw = expendService.exportDosageExpend(sheetName, title, expendVoList);
//			HSSFWorkbook hw = null;
			if (hw == null)
				return RestResultDto.fail("没有导出数据~！");
			//响应到客户端
			OutputStream os = response.getOutputStream();
			try {
				this.setResponseHeader(response, sheetName + ".xlsx");
				hw.write(os);
				os.flush();
				os.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				os.close();
			}
			return RestResultDto.success(expendVoList);
		} catch (Exception e) {
			e.printStackTrace();
			return RestResultDto.fail("没有导出数据~！");
		}
	}
	/**
	 * 导出料仓消耗统计
	 * @param orgId
	 * @param lineId 生产线ID
	 * @param storageId 仓位ID
	 * @param materialId 材料ID
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/storage-expend-export", method = RequestMethod.POST)
	public RestResultDto storageExpend(HttpServletRequest request, HttpServletResponse response, Long orgId, Long lineId, Long storageId, Long materialId,
									   String startTime, String endTime, String orgName, String line,
									   String storage, String material, Integer type, Integer materialType, Integer breedType) {
		try {
			List<LabExpendVo> expendVoList = expendService.findStorageExpend(orgId, lineId, storageId, materialId, startTime, endTime,type, materialType, breedType);
			if (expendVoList == null || expendVoList.size() == 0) {
				return RestResultDto.fail("没有任何数据");
			}
			CacheUser user= getUser();
			String sheetName = orgName + "料仓消耗统计汇总表(" + startTime.substring(0,10) + "~" + endTime.substring(0, 10) + ")";
			String title = "制表人:" + user.getName() + ",导出时间:" + DateUtil.convertString(new Date());
			XSSFWorkbook hw = expendService.exportStorageExpend(sheetName, title, expendVoList,type);
//			HSSFWorkbook hw = null;
			if (hw == null)
				return RestResultDto.fail("没有导出数据~！");
			//响应到客户端
			OutputStream os = response.getOutputStream();
			try {
				this.setResponseHeader(response, sheetName + ".xlsx");
				hw.write(os);
				os.flush();
				os.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				os.close();
			}
			return RestResultDto.success(expendVoList);
		} catch (Exception e) {
			e.printStackTrace();
			return RestResultDto.fail("没有导出数据~！");
		}
	}

	/**
	 * 查询发货单的盘消耗数据
	 *
	 * @param invoiceId
	 * @return
	 */
	@RequestMapping(value = "/invoice/expend/{invoiceId}", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto invoiceExpend(@PathVariable Long invoiceId) {
		if (invoiceId == null) {
			return RestResultDto.fail();
		}
		//	查询发货单数据
		ProductionInvoice invoice = invoiceService.getById(invoiceId);
		if (!BaseEntity.validateExist(invoice)) {
			return RestResultDto.fail("发货单不存在");
		}
		//	查询发货单的盘数据
		List<LabExpendBatchVo> batchList = expendBatchService.queryDetailByInv(erpDb, invoiceId);
		return RestResultDto.success(batchList);
	}

}



